/**************************************************************
 * Copyright (c) 2021 anxinsec.com, Inc. All Rights Reserved
 * User: zhangdongsheng<zhangdongsheng@anxinsec.com>
 * Date: 2021/11/17
 * Desc:
 **************************************************************/

package utils

import (
	"bytes"
	"compress/zlib"
	"io"
)

// DoZlibCompress ZIP压缩数据
func DoZlibCompress(src []byte) ([]byte, error) {
	var in bytes.Buffer
	//w := zlib.NewWriter(&in)
	w, err := zlib.NewWriterLevel(&in, zlib.BestSpeed)
	if err != nil {
		return nil, err
	}
	_, err = w.Write(src)
	if err != nil {
		return nil, err
	}
	err = w.Close()
	if err != nil {
		return nil, err
	}
	return in.Bytes(), nil
}

// DoZlibUnCompress ZIP解压数据
func DoZlibUnCompress(compressSrc []byte) ([]byte, error) {
	b := bytes.NewReader(compressSrc)
	var out bytes.Buffer
	r, err := zlib.NewReader(b)
	if err != nil {
		return nil, err
	}
	_, err = io.Copy(&out, r)
	if err != nil {
		return nil, err
	}
	return out.Bytes(), nil
}
